# 1、导包
import pymysql
# 2、创建连接，连接数据库
conn = pymysql.connect(user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
        password="123456",
        host="localhost",
        database="books",
        port = 3306)
# 3、获取游标
cursor = conn.cursor()

sql = "select * from t_book"
# 4、执行sql
cursor.execute(sql)

# 获取游标第一行数据
print("获取查询一行数据前游标的位置：",cursor.rownumber)
result1 = cursor.fetchone()
print(result1)


# 5、获取全部查询数据
print("获取查询所有数据前游标的位置：",cursor.rownumber)
# 重置游标位置
cursor.rownumber = 0
result = cursor.fetchall()
print(result)

print("获取所有数据前游标的位置：",cursor.rowcount)
# 6、关闭游标
cursor.close()
# 7、关闭连接
conn.close()
